package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.ProProcessVo;
import com.ruoyi.system.domain.bo.ProProcessBo;
import com.ruoyi.system.service.IProProcessService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 执行记录
 *
 * @author ruoyi
 * @date 2023-05-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/process")
public class ProProcessController extends BaseController {

    private final IProProcessService iProProcessService;

    /**
     * 查询执行记录列表
     */
    @GetMapping("/list")
    public TableDataInfo<ProProcessVo> list(ProProcessBo bo, PageQuery pageQuery) {
        return iProProcessService.queryPageList(bo, pageQuery);
    }
    /**
     * 查询执行记录列表
     */
    @GetMapping("/findAllByTaskId")
    public R<List<ProProcessVo>> findAllByTaskId(@RequestParam Long taskId) {
        ProProcessBo bo = new ProProcessBo();
        bo.setTaskId(taskId);
        List<ProProcessVo> list = iProProcessService.queryList(bo);
        return R.ok(list);
    }


    /**
     * 查询执行记录列表
     */
    @GetMapping("/findAllBySubjectId")
    public R<List<ProProcessVo>> findAllBySubjectId(@RequestParam Long subjectId) {
        ProProcessBo bo = new ProProcessBo();
        bo.setSubjectId(subjectId);
        List<ProProcessVo> list = iProProcessService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出执行记录列表
     */
    @Log(title = "执行记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProProcessBo bo, HttpServletResponse response) {
        List<ProProcessVo> list = iProProcessService.queryList(bo);
        ExcelUtil.exportExcel(list, "执行记录", ProProcessVo.class, response);
    }

    /**
     * 获取执行记录详细信息
     *
     * @param processId 主键
     */
    @GetMapping("/{processId}")
    public R<ProProcessVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long processId) {
        return R.ok(iProProcessService.queryById(processId));
    }

    /**
     * 新增执行记录
     */
    @Log(title = "执行记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProProcessBo bo) {
        return toAjax(iProProcessService.insertByBo(bo));
    }

    /**
     * 修改执行记录
     */
    @Log(title = "执行记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProProcessBo bo) {
        return toAjax(iProProcessService.updateByBo(bo));
    }

    /**
     * 删除执行记录
     *
     * @param processIds 主键串
     */
    @Log(title = "执行记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{processIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] processIds) {
        return toAjax(iProProcessService.deleteWithValidByIds(Arrays.asList(processIds), true));
    }
}
